package oc.tm.sg.admin.web.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.smart4j.framework.tx.annotation.Service;

import oc.tm.sg.admin.govern.BootstrapGovern;
import oc.tm.sg.admin.govern.domain.SgHost;
import oc.tm.sg.admin.web.service.HostsService;
import oc.tm.sg.core.util.NetUtils;
import oc.tm.sg.core.util.StringUtils;
import oc.tm.sg.registry.common.Constants;
import oc.tm.sg.registry.common.URL;

/**
 * 主机控制器
 * @author lry
 */
@Service
public class HostsServiceImpl implements HostsService {
	BootstrapGovern registryContainer=BootstrapGovern.INSTANCE;
    /**
     * 获取所有的主机列表
     * @return
     */
    public List<SgHost> getHosts() {
        List<SgHost> rows = new ArrayList<SgHost>();
        Set<String> hosts = registryContainer.getHosts();

        if (hosts != null && hosts.size() > 0) {
            SgHost dubboHost;
            for (String host : hosts) {
                dubboHost = new SgHost();
                dubboHost.setHost(host);
                dubboHost.setHostname(NetUtils.getLocalAddress(host));

                List<URL> providers = registryContainer.getProvidersByHost(host);
                List<URL> consumers = registryContainer.getConsumersByHost(host);
                if ((providers != null && providers.size() > 0) || (consumers != null && consumers.size() > 0)) {
                	String application=null,owner=null,organization=null;
                	List<URL> eList=new ArrayList<URL>();
                	eList.addAll(providers);
                	eList.addAll(consumers);
                    for (URL eurl:eList) {
						if(StringUtils.isBlank(application)){//应用
							application=eurl.getParameter(Constants.APPLICATION_KEY, "");
							dubboHost.setApplication(application);
						}
						if(StringUtils.isBlank(owner)){//负责人
							owner=eurl.getParameter("owner", "");
							dubboHost.setOwner(owner);
						}
						if(StringUtils.isBlank(organization)){//所属组织
							organization=eurl.getParameter("organization", "");
							dubboHost.setOrganization(organization);
						}
						if(StringUtils.isNotBlank(application)&&StringUtils.isNotBlank(
								owner)&&StringUtils.isNotBlank(organization)){
							break;
						}
					}
                }

                int providerSize = providers == null ? 0 : providers.size();
                dubboHost.setProviderCount(providerSize);
                int consumerSize = consumers == null ? 0 : consumers.size();
                dubboHost.setConsumerCount(consumerSize);

                rows.add(dubboHost);
            }
        }
        return rows;
    }

    /**
     * 根据主机获取服务提供者列表
     * @param host
     * @return
     */
    public List<String> providers(String host) {
        List<URL> providers = registryContainer.getProvidersByHost(host);
        List<String> rows = new ArrayList<String>();
        if (providers != null && providers.size() > 0) {
            for (URL u : providers) {
                rows.add(u.toFullString());
            }
        }
        return rows;
    }

    /**
     * 根据主机获取服务消费者列表
     * @param host
     * @return
     */
    public List<String> consumers(String host) {
        List<URL> consumers = registryContainer.getConsumersByHost(host);
        List<String> rows = new ArrayList<String>();
        if (consumers != null && consumers.size() > 0) {
            for (URL u : consumers) {
                rows.add(u.toFullString());
            }
        }
        return rows;
    }

}
